package com.bootdo.kaoqin.thread;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.kaoqin.domain.KaoqinCallGroup;
import com.bootdo.kaoqin.domain.KaoqinConfig;
import com.bootdo.kaoqin.domain.KaoqinOrder;
import com.bootdo.kaoqin.domain.KaoqinUser;
import java.util.ArrayList;
import java.util.List;

public class HjbTask implements Runnable{

    private ComputeThread computeThread;
    private KaoqinCallGroup kaoqinCallGroup;
    private String  nowTime;
    public HjbTask(ComputeThread computeThread,KaoqinCallGroup kaoqinCallGroup,String nowTime){
        this.computeThread = computeThread;
        this.kaoqinCallGroup = kaoqinCallGroup;
        this.nowTime = nowTime;
    }

    @Override
    public void run() {
        try {
            List<KaoqinUser> kaoqinUsers = computeThread.getKaoqinUserService().list(MapBuild.getInstand().setKV("groupId",kaoqinCallGroup.getId()));
            for(KaoqinUser kaoqinUser : kaoqinUsers){
                kaoQinTongji(kaoqinUser);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    void kaoQinTongji(KaoqinUser kaoqinUser){
        List<KaoqinConfig> kaoqinConfigs = computeThread.getKaoqinConfigService().list(MapBuild.getInstand().setKV("groupId", kaoqinCallGroup.getId()));
        List<KaoqinConfig> notCallList = new ArrayList<KaoqinConfig>();
        for(KaoqinConfig kaoqinConfig : kaoqinConfigs){
            List<KaoqinOrder> kaoqinOrders = computeThread.getKaoqinOrderService()
                    .list(MapBuild.getInstand()
                            .setKV("configId", kaoqinConfig.getId())
                            .setKV("createDate", nowTime)
                            .setKV("empId",kaoqinUser.getId())
                    );
            if(null == kaoqinOrders || kaoqinOrders.size() <= 0){
                //发送打卡异常消息
                notCallList.add(kaoqinConfig);
            }
            excepTionHander(notCallList);
        }
    }
    void  excepTionHander( List<KaoqinConfig> notCallList ){
        if(notCallList.size() <= 0)
            return;
        for(KaoqinConfig kaoqinConfig : notCallList){
            sendExcepTion(kaoqinConfig);
        }
    }
    void sendExcepTion(KaoqinConfig kaoqinConfig){
        System.err.println(kaoqinConfig.getName());
    }
}
